package com.imay.live.adapter;

import android.support.annotation.LayoutRes;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.imay.live.R;
import com.imay.live.cache.UserInfoCache;
import com.imay.live.fragment.dynamics.DynamicsParams;
import com.imay.live.helper.LevelHelper;
import com.imay.live.model.FeedModel;
import com.imay.live.model.ImayUserMoreModel;
import com.imay.live.model.RoomInfoModel;
import com.imay.live.model.UserInfoModel;
import com.imay.live.model.UserModel;
import com.imay.live.utils.GlideUtil;
import com.imay.live.view.GridImageLayout;

import java.util.List;

/**
 * Created by liangzhenxiong on 2017/4/6 0006.
 * 发现-分类
 */

public class ImayCelebrityAdapter extends BaseQuickAdapter<Object, BaseViewHolder> {

    long myuid;
    private SparseArray<Integer> layouts;
    public final static int VIEWTYPE_LINEAR_HEADER = 3;//名人多列头像

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public ImayCelebrityAdapter(List<Object> data) {
        super(data);
        addItemType(VIEWTYPE_LINEAR_HEADER, R.layout.celebrity_image);
        UserInfoModel userInfoModel = UserInfoCache.getUserInfo();
        if (userInfoModel != null && userInfoModel.getUser() != null) {
            myuid = userInfoModel.getUser().getUid();
        }
    }

    protected void addItemType(int type, @LayoutRes int layoutResId) {
        if (layouts == null) {
            layouts = new SparseArray<>();
        }
        layouts.put(type, layoutResId);
    }


    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        return createBaseViewHolder(parent, getLayoutId(viewType));
    }

    private int getLayoutId(int viewType) {
        return layouts.get(viewType);
    }

    @Override
    protected int getDefItemViewType(int position) {
        if (!mData.isEmpty()) {
            Object item = mData.get(position);
            if (item instanceof FeedModel) {
                FeedModel feedModel = (FeedModel) item;
                if (feedModel.getItemType() == DynamicsParams.VIEWTYPE_LINEAR_IMAGE) {
                    return DynamicsParams.VIEWTYPE_LINEAR_IMAGE;
                } else if (feedModel.getItemType() == DynamicsParams.VIEWTYPE_LINEAR_VIDEO) {
                    return DynamicsParams.VIEWTYPE_LINEAR_VIDEO;
                }
            } else if (item instanceof RoomInfoModel) {
                return DynamicsParams.VIEWTYPE_LINEAR_LIVE;
            } else if (item instanceof ImayUserMoreModel) {
                return VIEWTYPE_LINEAR_HEADER;
            }
        }
        return DynamicsParams.VIEWTYPE_NONE;
    }


    @Override
    protected void convert(final BaseViewHolder helper, Object item) {
        MultiItemEntity entity = (MultiItemEntity) item;
        if (entity.getItemType() == VIEWTYPE_LINEAR_HEADER) {
            if (item instanceof ImayUserMoreModel) {
                ImayUserMoreModel imayUserMoreModel = (ImayUserMoreModel) item;
                UserModel user = imayUserMoreModel.getUser();
                if (user != null) {
                    helper.addOnClickListener(R.id.iv_face);
                    helper.addOnClickListener(R.id.tv_name);
                    if (user.getUid() == myuid) {
                        helper.setVisible(R.id.tv_fllow, false);
                    } else {
                        helper.addOnClickListener(R.id.tv_fllow);
                        helper.setVisible(R.id.tv_fllow, true);
                    }
                    if (user.isMyFollow()) {
                        helper.setTextColor(R.id.tv_fllow, helper.getConvertView().getResources().getColor(R.color.text_b2));
                        helper.setText(R.id.tv_fllow, helper.getConvertView().getResources().getString(R.string.has_followed));
                        helper.setBackgroundRes(R.id.tv_fllow, R.drawable.shape_corner5_white_bc);
                    } else {
                        helper.setTextColor(R.id.tv_fllow, helper.getConvertView().getResources().getColor(R.color.color_ff));
                        helper.setText(R.id.tv_fllow, helper.getConvertView().getResources().getString(R.string.un_followed));
                        helper.setBackgroundRes(R.id.tv_fllow, R.drawable.shape_corner100_white_ff0);
                    }
                    TextView tv_name = helper.getView(R.id.tv_name);
//                    helper.setText(R.id.tv_content, user.getIntro());
                    ImageView iv_v = helper.getView(R.id.iv_v);
                    int resId = LevelHelper.getVIPIcon(user.getVip());
                    if (resId != 0) {
                        iv_v.setVisibility(View.VISIBLE);
                        iv_v.setImageResource(resId);
                    } else {
                        iv_v.setVisibility(View.GONE);
                    }
                    tv_name.setText(user.getNick());
                    if (!TextUtils.isEmpty(user.getImgHead())) {
                        ImageView iv_face = helper.getView(R.id.iv_face);
                        GlideUtil.loadCircleImage(iv_face,user.getImgHead(),R.mipmap.pnl_00_head108);
                    }
                }
                GridImageLayout gridImageLayout = helper.getView(R.id.grid_head);
                gridImageLayout.setModels(imayUserMoreModel.getFeeds(), helper.getConvertView().getResources().getString(R.string.str_imay_photo));
            }

        } else {

        }
    }
}
